package com.zyf.chapter01.principle.DRY.refactor;

import com.zyf.chapter01.principle.DRY.User;
import com.zyf.chapter01.principle.DRY.utils.EmailValidation;
import com.zyf.chapter01.principle.DRY.utils.PasswordValidation;

/**
 * 用户服务类
 * 重构后，将参数校验都放到服务类中
 * @author yifeng
 */
public class UserService {
    private UserRepo userRepo; // 通过依赖注入 或者 IOC 框架注入

    public User login(String email, String password) {
        if (!EmailValidation.validate(email)) {
            // ... throw InvalidEmailException ...
        }
        if (!PasswordValidation.validate(password)) {
            // ... throw InvalidPasswordException ...
        }
        User user = userRepo.getUserByEmail(email);
        if (user == null || !password.equals(user.getPassword())) {
            // ... throw InvalidFailureException ...
        }
        return user;
    }
}
